import 'dart:async';
import 'dart:ffi';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mybase/base/tool/globalMethod/FFSafeValue.dart';

import 'apple_maps_platform_interface.dart';

/// An implementation of [AppleMapsPlatform] that uses method channels.
class MethodChannelAppleMaps extends AppleMapsPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('apple_maps');

  final eventChannel = const EventChannel('apple_maps_eventChannel');

  /// 流
  final StreamController<double> airPlayStatusStream = StreamController<double>.broadcast();

  MethodChannelAppleMaps() {
    eventChannel.receiveBroadcastStream().listen((event) {
      String? type = globalSafeValueFromMap(event, "type");

      if (type == "heading") {
        num? status = globalSafeValueFromMap(event, "heading") ?? 0;
        airPlayStatusStream.add(status.toDouble());
      }
    });
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<void> startCompass() async {
    await methodChannel.invokeMethod<String>('startCompass');
  }

  @override
  Future<void> stopCompass() async {
    await methodChannel.invokeMethod<String>('stopCompass');
  }

  @override
  StreamController<double> getCompassStream() {
    return airPlayStatusStream;
  }
}
